package com.kobeliu.entity;

import java.util.ArrayList;
import java.util.List;

public class QuickSortDemo {

    public static void main(String[] args) {

        int[] arr = {3,5,6};
        List<Integer> nums = new ArrayList<>();
//        quickSort(arr,0,arr.length-1);
//
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
//        }
        nums.add(1);
        nums.add(2);
        nums.add(3);
        nums.add(4);
        System.out.println(fun(nums));
    }

    private static void quickSort(int[] arr,int left,int right) {
        if(left<right){
            //随机找一个位置上的数 与末尾做交换
            swap(arr,left+(int)(Math.random()*(right-left+1)),right);
            int[] p = partition(arr,left,right);
            quickSort(arr,0,p[0]-1);
            quickSort(arr,p[1]+1,right);
        }
    }

    private static int[] partition(int[] arr,int left,int right) {
        int[] p = new int[2];

        int temp = arr[right];
        int i = left;
        int L = left-1;
        int R = right;
        while(i<R) {
            if(arr[i]<temp){
                swap(arr,i++,++L);
            }else if(arr[i]>temp){
                swap(arr,i,--R);
            }else{
                i++;
            }
        }
        swap(arr,L+1,right);
        p[0] = L+1;
        p[1] = R-1;
        return p;

    }

    private static void swap(int[] arr, int left, int right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
    }

    static Double fun(List<Integer> nums){

        if(nums.size() == 0) return (double)0;
        if(nums.size()%2 == 1){
            //此时数组为奇数，取中间一个
            Integer n = nums.size() / 2;
            return (double) nums.get(n);
        }else{
            int sum = nums.get(nums.size() / 2)+nums.get(nums.size() / 2 - 1);
            return (double)sum/2;

        }
    }
}
